home *** CD-ROM | disk | FTP | other *** search
- -fun map f [] = []
- | map f (a::x) = (f a) :: (map f x)
- >val map = fn : ('a -> 'b) -> 'a list -> 'b list
- (* the function map takes a function and a list as argument and *)
- (* applies the function to all elements in the list. *)
- (* ie : -map square[1,3,5]; >[1,9,25] *)
-
- -fun twice f = f o f;
- >val twice = fn : ('a -> 'a) -> 'a -> 'a
- (* the compose operator 'o' is a standard in ML and has lower *)
- (* precedence than any other standard operator *)
- (* ie : twice sqr 5 ->(sqr o sqr)5 ->sqr(sqr 5) ->sqr 25 ->625 *)
- other ie :
- -fun last = hd o rev;
- >val last = fn : 'a list -> 'a
-
- -fun second = hd o tl;
- >val second = fn : 'a list -> 'a
-
- -fun third = hd o tl o tl;
- >val third = fn : 'a list -> 'a
- (* ie : third[1,3,5] -> (hd o tl o tl)[1,3,5] -> *)
- (* (hd o tl)(tl[1,3,5]) -> (hd o tl)[3,5] -> *)
- (* hd(tl[3,5]) -> hd[5] -> 5 *)